﻿/***********************************************************************
 *            Project: CoreCms
 *        ProjectName: 核心内容管理系统                                
 *                Web: https://www.corecms.net                      
 *             Author: 大灰灰                                          
 *              Email: jianweie@163.com                                
 *         CreateTime: 2021/1/31 21:45:10
 *        Description: 暂无
 ***********************************************************************/


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using AutoMapper;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Model.ViewModels.DTO;
using CoreCms.Net.Utility.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SqlSugar;
using Org.BouncyCastle.Cms;
using Org.BouncyCastle.Ocsp;
using static SKIT.FlurlHttpClient.Wechat.Api.Models.ProductSPUGetListResponse.Types;
using NPOI.SS.Formula.Functions;
using MathNet.Numerics;
using NuGet.Packaging;
using NPOI.XSSF.UserModel;

namespace CoreCms.Net.Web.WebApi.Controllers
{
    /// <summary>
    /// 商品相关接口处理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GoodController : ControllerBase
    {
        private IMapper _mapper;
        private readonly IHttpContextUser _user;

        private ICoreCmsSettingServices _settingServices;
        private ICoreCmsGoodsCategoryServices _goodsCategoryServices;
        private ICoreCmsGoodsServices _goodsServices;
        private ICoreCmsProductsServices _productsServices;
        private ICoreCmsBrandServices _brandServices;
        private ICoreCmsOrderItemServices _orderItemServices;
        private ICoreCmsGoodsCommentServices _goodsCommentServices;
        private ICoreCmsGoodsParamsServices _goodsParamsServices;
        private ICoreCmsGoodsCollectionServices _goodsCollectionServices;
        private ICoreCmsUserServices _userServices;
        private  ICoreShopOrderMeetingUserServices _coreShopOrderMeetingUserServices;
        private  ICoreShopOrderMeetingGoodsServices _CoreShopOrderMeetingGoodsServices;

        /// <summary>
        /// 构造函数
        /// </summary>
        public GoodController(IMapper mapper
            , IHttpContextUser user
            , ICoreCmsSettingServices settingServices
            , ICoreCmsGoodsCategoryServices goodsCategoryServices
            , ICoreCmsGoodsServices goodsServices
            , ICoreCmsProductsServices productsServices
            , ICoreCmsBrandServices brandServices
            , ICoreCmsOrderItemServices orderItemServices
            , ICoreCmsGoodsCommentServices goodsCommentServices
            , ICoreCmsGoodsParamsServices goodsParamsServices
            , ICoreCmsGoodsCollectionServices goodsCollectionServices
            , ICoreCmsUserServices userServices
                , ICoreShopOrderMeetingUserServices coreShopOrderMeetingUserServices
              , ICoreShopOrderMeetingGoodsServices CoreShopOrderMeetingGoodsServices
        )
        {
            _mapper = mapper;
            _user = user;
            _settingServices = settingServices;
            _goodsCategoryServices = goodsCategoryServices;
            _goodsServices = goodsServices;
            _productsServices = productsServices;
            _brandServices = brandServices;
            _orderItemServices = orderItemServices;
            _goodsCommentServices = goodsCommentServices;
            _goodsParamsServices = goodsParamsServices;
            _goodsCollectionServices = goodsCollectionServices;
            _userServices = userServices;
            _coreShopOrderMeetingUserServices = coreShopOrderMeetingUserServices;
            _CoreShopOrderMeetingGoodsServices = CoreShopOrderMeetingGoodsServices;

        }

        //公共接口====================================================================================================

        /// <summary>
        /// 获取sku
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetSkus([FromBody]  dynamic obj)
        {
            int goodsid = int.Parse((string)obj["goodsid"]);
            int colorid = int.Parse((string)obj["colorid"]+"");
            var jm = new WebApiCallBack();
            var plist =   _productsServices.GetDb().Queryable<CoreCmsProducts>()
                .Where(m => m.goodsId == goodsid && m.colorId == colorid)
                .Includes( prd => prd.color)
                .Includes( prd => prd.size).ToList();

            List<SizeSpesDesc> relist = new List<SizeSpesDesc>();
            foreach (var item in plist)
            {
                relist.Add(new SizeSpesDesc
                {
                     color= item.color.value,
                      productId=item.id,
                       size=item.size.value,
                        sizeId=item.sizeId,
                     stock=item.stock
                      
                });
            }
            
            jm.data = relist;
            jm.status = true;

            return jm;
        }

        #region 获取所有商品分类栏目数据
       /// <summary>
       /// 获取所有商品分类栏目数据
       /// </summary>
       /// <returns></returns>
       [HttpPost]
        public async Task<WebApiCallBack> GetAllCategories()
        {
            var jm = new WebApiCallBack() { status = true };

            var data = await _goodsCategoryServices.QueryListByClauseAsync(p => p.isShow == true, p => p.sort,
                OrderByType.Asc);
            var wxGoodCategoryDto = new List<WxGoodCategoryDto>();

            //第一级
            var isfitst = data.Where(p => p.parentId == 0).ToList();
            if (isfitst.Any())
            {
                isfitst.ForEach(p => 
                {
                    var fitst = new WxGoodCategoryDto();
                    fitst.id = p.id;
                    fitst.name = p.name;
                    fitst.imageUrl = !string.IsNullOrEmpty(p.imageUrl) ? p.imageUrl : "/static/images/common/empty.png";
                    fitst.sort = p.sort;
                    fitst.child = new List<WxGoodCategoryChild>();
                    #region 第二级
                    var secList = data.Where(m => m.parentId == p.id).ToList();
                    if (secList.Any())
                    {
                        secList.ForEach(sp =>
                        {
                            var sec = new WxGoodCategoryChild();
                            sec.id = sp.id;
                            sec.name = sp.name;
                            sec.imageUrl = !string.IsNullOrEmpty(sp.imageUrl) ? sp.imageUrl : "/static/images/common/empty.png";
                            sec.sort = sp.sort;
                            sec.child = new List<WxGoodCategoryChild>();
                            #region 第三级
                            var tList = data.Where(m => m.parentId == sp.id).ToList();
                            if (tList.Any())
                            {
                                tList.ForEach(tp =>
                                {
                                    var thid=new WxGoodCategoryChild();
                                    thid.id = tp.id;
                                    thid.name = tp.name;
                                    thid.imageUrl = !string.IsNullOrEmpty(tp.imageUrl) ? tp.imageUrl : "/static/images/common/empty.png";
                                    thid.sort = tp.sort;
                                    sec.child.Add(thid);
                                });

                            }

                            #endregion
                            fitst.child.Add(sec);

                        });
                        
                    }


                    #endregion

                    wxGoodCategoryDto.Add(fitst);
                });
           
            }
          




            jm.status = true;
            jm.data = wxGoodCategoryDto;

            return jm;
        }

        [HttpGet]

        public async Task<WebApiCallBack> GetPCategories()
        {
            var jm = new WebApiCallBack() { status = true };

            var data = await _goodsCategoryServices.QueryListByClauseAsync(p => (p.isShow == true&& p.parentId==0), p => p.sort,
                OrderByType.Asc);
       
            jm.status = true;
            jm.data = data;

            return jm;
        }

        #endregion

        #region 根据查询条件获取分页数据============================================================
        /// <summary>
        /// 根据查询条件获取分页数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetGoodsPageList([FromBody] FMPageByWhereOrder entity)
        {
            var jm = new WebApiCallBack();
            var where = PredicateBuilder.True<CoreCmsGoods>();
            var className = string.Empty;
            int meetingid = 0;

            if (!string.IsNullOrWhiteSpace(entity.where))
            {
                var obj = JsonConvert.DeserializeAnonymousType(entity.where, new
                {
                    brandId = "",
                    catId = "",
                    designerId = "",
                    searchWords = "",
                    meetingid = "",
                    priceFrom = "",
                    priceTo = "",
                    //
                    labelId = "",

                });

                if (!string.IsNullOrWhiteSpace(obj.priceFrom))
                {
                    var priceF = obj.priceFrom.ObjectToDouble(0);
                    if (priceF >= 0)
                    {
                        var f = Convert.ToDecimal(priceF);
                        where = where.And(p => p.price >= f);
                    }
                }
                if (!string.IsNullOrWhiteSpace(obj.priceTo))
                {
                    var priceT = obj.priceTo.ObjectToDouble(0);
                    if (priceT > 0)
                    {
                        var f = Convert.ToDecimal(priceT);
                        where = where.And(p => p.price <= f);
                    }
                }
                if (!string.IsNullOrWhiteSpace(obj.catId))
                {
                    var catId = obj.catId.ObjectToInt(0);

                    if (catId > 0)
                    {
                        List<int> ids = new List<int>();
                        ids.Add(catId); //第一级
                        var category = await _goodsCategoryServices.QueryByIdAsync(catId);
                        if (category != null)
                        {
                            className = category.name;
                        }

                        var childs = await _goodsCategoryServices.QueryListByClauseAsync(p => p.parentId == catId);
                        if (childs.Any())
                        {
                            var se_ids = childs.Select(p => p.id).ToList();
                            ids.AddRange(se_ids); //第二级
                            foreach (var se in se_ids)
                            {
                                var th_lds = await _goodsCategoryServices.QueryListByClauseAsync(p => p.parentId == se);
                                if (th_lds.Any())
                                {
                                    ids.AddRange(th_lds.Select(p => p.id).ToList());//第三级
                                }

                            }

                        }


                        where = where.And(p => ids.Contains(p.goodsCategoryId));
                    }
                }
                if (!string.IsNullOrWhiteSpace(obj.brandId))
                {
                    var brandId = obj.brandId.ObjectToInt(0);
                    if (brandId > 0)
                    {
                        where = where.And(p => p.brandId == brandId);
                    }
                }
                if (!string.IsNullOrWhiteSpace(obj.designerId))
                {
                    var designerId = obj.designerId.ObjectToInt(0);
                    if (designerId > 0)
                    {
                        where = where.And(p => p.designerId == designerId);
                    }
                }
                //if (!string.IsNullOrWhiteSpace(obj.labelId))
                //{
                //    where = where.And(p => (',' + p.labelIds.Trim(',') + ',').Contains(',' + obj.labelId.Trim(',') + ','));
                //}
                if (!string.IsNullOrWhiteSpace(obj.searchWords))
                {
                    where = where.And(p => p.name.Contains(obj.searchWords));
                }


                if (!string.IsNullOrWhiteSpace(obj.searchWords))
                {
                    where = where.And(p => p.name.Contains(obj.searchWords));
                }



                if (!string.IsNullOrWhiteSpace(obj.meetingid))
                {

                    meetingid = int.Parse(obj.meetingid);



                }
              

            }
            if (meetingid == 0)
            {
                where = where.And(p => p.isMarketable == true);
            }
        

            where = where.And(p => p.isDel == false);
         

       

            var orderBy = " isRecommend desc,isHot desc";
            if (!string.IsNullOrWhiteSpace(entity.order))
            {
                orderBy += "," + entity.order;
            }



            //获取数据
           var  list = await _goodsServices.QueryPageAsync(where, orderBy, entity.page, entity.limit, false, meetingid);
         


            //返回数据
            jm.status = true;
            jm.data = new
            {
                list,
                className,
                entity.page,
                list.TotalCount,
                list.TotalPages,
                entity.limit,
                entity.where,
                entity.order,
             //   brands
            };
            jm.msg = "数据调用成功!";

            return jm;
        }
        #endregion

        #region 获取商品详情======================================================================
        /// <summary>
        /// 获取商品详情
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetDetial([FromBody] FMIntId entity)
        {
            var jm = new WebApiCallBack();

            var userId = 0;
            if (_user != null)
            {
                userId = _user.ID;
            }
            else
            {
                
            }
            #region 我加的判断订货会权限
           int  meetingid;
            try {
                 meetingid = (int)(entity.data["orderMeetingId"]);
            }
            catch (Exception e) {
                 meetingid = 0;
            }
       
            if (meetingid > 0)
            {
                if (_user == null) //是否登录帐号
                {
                    jm.status = false;
                    jm.msg = "未登录";
                    jm.code = 401;
                    return jm;
                }
                else 
                {
                    var meetinguser = await _coreShopOrderMeetingUserServices.QueryIsAllow(meetingid, _user.ID);
                    if (meetinguser == null)
                    {
                        jm.status = false;
                        jm.msg = "没有订货会权限";
                        jm.code = 401;
                        return jm;
                    }
                }
            }
            #endregion
        

       
            var model = await _goodsServices.GetGoodsDetial(entity.id, userId, false,"goods",0, meetingid);
            if (model == null)
            {
                jm.msg = "商品获取失败";
                return jm;
            }

            jm.status = true;
            jm.msg = "获取商品详情成功";
            jm.data = model;
            jm.methodDescription = JsonConvert.SerializeObject(_user);

            return jm;
        }
        #endregion


        #region 获取单个货品信息======================================================================
        /// <summary>
        /// 获取单个货品信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetProductInfo([FromBody] FMGetProductInfo entity)
        {
            var jm = new WebApiCallBack();

            var userId = 0;
            if (_user != null)
            {
                userId = _user.ID;
            }

            bool bl = entity.type == "pinTuan" || entity.type == "group";

            var getProductInfo = await _productsServices.GetProductInfo(entity.id, bl, userId, entity.type, entity.groupId);
            if (getProductInfo == null)
            {
                jm.msg = "获取单个货品失败";
                return jm;
            }

            jm.status = true;
            jm.msg = "获取单个货品成功";
            jm.data = getProductInfo;

            return jm;
        }

        #endregion

        #region 获取商品评价列表分页数据======================================================================
        /// <summary>
        /// 获取商品评价列表分页数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetGoodsComment([FromBody] FMPageByIntId entity)
        {
            var jm = new WebApiCallBack();

            //获取数据
            var list = await _goodsCommentServices.QueryPageAsync(p => p.goodsId == entity.id && p.isDisplay == true, p => p.createTime, OrderByType.Desc, entity.page, entity.limit);

            if (list.Any())
            {
                foreach (var item in list)
                {
                    item.imagesArr = !string.IsNullOrEmpty(item.images) ? item.images.Split(",") : null;
                }
            }

            jm.status = true;
            jm.msg = "获取评论成功";
            jm.data = new
            {
                list,
                commentsCount = list.TotalCount,
                totalPages = list.TotalPages
            };

            return jm;
        }
        #endregion

        #region 获取商品参数======================================================================
        /// <summary>
        /// 获取单个商品参数
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetGoodsParams([FromBody] FMIntId entity)
        {
            var jm = new WebApiCallBack();

            //获取数据
            var goods = await _goodsServices.QueryByIdAsync(entity.id);
            if (goods == null)
            {
                jm.msg = GlobalConstVars.DataisNo;
                return jm;
            }
            var list = new List<WxNameValueDto>();
            var goodsParams = await _goodsParamsServices.QueryAsync();

            if (!string.IsNullOrEmpty(goods.parameters))
            {
                var arrItem = goods.parameters.Split("|");
                foreach (var item in arrItem)
                {
                    if (!item.Contains(":")) continue;

                    var childArr = item.Split(":");
                    if (childArr.Length == 2)
                    {
                        var paramsId = Convert.ToInt32(childArr[0]);
                        var paramsModel = goodsParams.First(p => p.id == paramsId);
                        if (paramsModel != null)
                        {
                            list.Add(new WxNameValueDto()
                            {
                                name = paramsModel.name,
                                value = childArr[1]
                            });
                        }
                    }
                }
            }
            jm.status = true;
            jm.msg = "获取商品参数成功";
            jm.data = list;

            return jm;
        }
        #endregion

        #region 获取随机推荐商品==================================================
        /// <summary>
        /// 获取随机推荐商品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetGoodsRecommendList([FromBody] FMIntId entity)
        {
            if (entity.id <= 0)
            {
                entity.id = 10;
            }

            var bl = entity.data.ObjectToBool();

            var jm = new WebApiCallBack()
            {
                status = true,
                code = 0,
                msg = "获取成功",
                data = await _goodsServices.GetGoodsRecommendList(entity.id, bl)
            };
            return jm;
        }
        #endregion

        //验证接口====================================================================================================


        #region 根据Token获取商品详情======================================================================
        /// <summary>
        /// 根据Token获取商品详情
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<WebApiCallBack> GetDetialByToken([FromBody] FMIntId entity)
        {
            var jm = new WebApiCallBack();

            var userId = 0;
            if (_user != null)
            {
                userId = _user.ID;
            }

            #region 我加的判断订货会权限
            int meetingid;
            try
            {
                meetingid = (int)(entity.data["orderMeetingId"]);
            }
            catch (Exception e)
            {
                meetingid = 0;
            }

            if (meetingid > 0)
            {
                if (_user == null) //是否登录帐号
                {
                    jm.status = false;
                    jm.msg = "未登录";
                    jm.code = 401;
                    return jm;
                }
                else
                {
                    var meetinguser = await _coreShopOrderMeetingUserServices.QueryIsAllow(meetingid, _user.ID);
                    if (meetinguser == null)
                    {
                        jm.status = false;
                        jm.msg = "没有订货会权限";
                        jm.code = 401;
                        return jm;
                    }
                }
            }
            #endregion


            var model = await _goodsServices.GetGoodsDetial(entity.id, userId, false, "goods",0, meetingid);
            if (model == null)
            {
                jm.msg = "商品获取失败";
                return jm;
            }

            await _goodsServices.UpdateAsync(p => new CoreCmsGoods() { viewCount = p.viewCount + 1 },
                p => p.id == entity.id);


            jm.status = true;
            jm.msg = "获取商品详情成功";
            jm.data = model;
            jm.methodDescription = JsonConvert.SerializeObject(_user);

            return jm;
        }
        #endregion



    }
}